# !/bin/bash
echo "----------------- 开始 --------------------"

set -e
APK_PATH="./android/app/build/outputs/apk/app-release.apk" # 安卓包路径
PWD=`pwd` # 当前目录
MSG=`git log -3 --date=format:'%Y-%m-%d %H:%M:%S' --pretty=format:"* %cd - %s "` # 最近 3 条更新

# GRADLE='https\:\/\/services.gradle.org\/distributions\/gradle-3.3-all.zip'
# sed -i '' 's/com.android.tools.build:gradle:3.0.1/com.android.tools.build:gradle:2.2.3/g' $PWD/android/build.gradle
# sed -i '' "s/\(distributionUrl=\)[^ ]*\(.*\)/\1${GRADLE}\2/g" $PWD/android/gradle/wrapper/gradle-wrapper.properties

echo "----------------- Android 打包开始 --------------------"



cd android

echo "清理 gradle 开始"

./gradlew clean

echo "---------清理 gradle 结束---------"

rm -rf app/build/

# yarn build

echo "./gradlew assembleRelease 开始"

./gradlew assembleRelease

cd ..

echo "----------------- Android 打包结束 --------------------"

echo "----------------- 上传蒲公英开始 --------------------"

echo "----------------- 上传 Android --------------------"

env=`cat /tmp/ENV.txt`

curl -F "file=@${PWD}/android/app/build/outputs/apk/app-release.apk" \
 -F "uKey=f700ce92efba61c69025108a1692959d" -F "_api_key=d643fccc60b8946ce543ef667562bebb" \
  -F "updateDescription=服务器环境:${env}\n${MSG}" https://qiniu-storage.pgyer.com/apiv1/app/upload > /tmp/upload_android.txt

echo "----------------- 上传蒲公英结束 --------------------"


echo "----------------- 发送到钉钉开始 --------------------"

node script/android.js

echo "----------------- 发送到钉钉结束 --------------------"

echo "----------------- 结束 --------------------"